package co.id.btel.create.pdf;

import java.io.FileOutputStream;

import utilities.CellModif;
import utilities.ChunkModif;
import utilities.Constants;
import utilities.PdfUtil;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfSummary {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String FILE = "E:/BTE/samplePdf/Summary.pdf";
		try {
			Document document = new Document();
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream(FILE));
			ChunkModif event = new ChunkModif();
			writer.setPageEvent(event);
			document.open();
			PdfUtil.addMetaData(document);
			addSummary(document);
			document.close();
			System.out.print("done!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param document
	 * @throws DocumentException
	 */
	public static void addSummary(Document document) throws DocumentException {

		CellModif event = new CellModif("roundRect", 0);
		PdfPTable table = new PdfPTable(9);
		table.setWidthPercentage(100);
		table.setWidths(new int[] { 10, 1, 10, 1, 10, 1, 10, 1, 10 });

		// sel 1
		Paragraph p = new Paragraph();
		Chunk ch = new Chunk("RINGKASAN TAGIHAN l ", FontFactory.getFont(
				FontFactory.HELVETICA, Constants.FONT_A, Font.NORMAL,
				BaseColor.WHITE));
		ch.setUnderline(Constants.AHA_BASE, 0.1f, 1, -7.5f, 1, 1);
		p.add(ch);

		ch = new Chunk("SUMMARY", FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_A, Font.ITALIC, BaseColor.WHITE));
		ch.setUnderline(Constants.AHA_BASE, 0.1f, 1, -7.5f, 1, 1);
		p.add(ch);
		PdfPCell c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setColspan(9);
		c1.setPadding(3);
		table.addCell(c1);

		// new line
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setColspan(9);
		table.addCell(c1);

		// sel 1
		p = new Paragraph();
		c1 = new PdfPCell(p);
		String[] t = { "Tagihan Sebelumnya |", "Previous Balance", "10.000.000" };
		c1.addElement(addSummaryInfo(t[0], t[1], t[2]));
		c1.setPadding(5f);
		// c1.setPaddingBottom(5f);
		// c1.setPaddingRight(3f);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidth(0);
		c1.setCellEvent(event);
		table.addCell(c1);

		// sel 2
		p = new Paragraph();
		ch = new Chunk("+", FontFactory.getFont(FontFactory.HELVETICA, 7,
				Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidth(0);
		c1.setPadding(0);
		table.addCell(c1);

		// sel 3
		p = new Paragraph();
		c1 = new PdfPCell(p);
		t = new String[] { "Pembayaran |", "Payment ", "2000" };
		c1.addElement(addSummaryInfo(t[0], t[1], t[2]));
		c1.setPadding(5f);
		// c1.setPaddingBottom(5f);
		// c1.setPaddingRight(3f);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidth(0);
		c1.setCellEvent(event);
		table.addCell(c1);

		// sel 4
		p = new Paragraph();
		ch = new Chunk("-", FontFactory.getFont(FontFactory.HELVETICA, 7,
				Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setPadding(0);
		table.addCell(c1);

		// sel 5
		p = new Paragraph();
		c1 = new PdfPCell(p);
		t = new String[] { "Tagihan Bulan Ini |", "Current Balance ", "100" };
		c1.addElement(addSummaryInfo(t[0], t[1], t[2]));
		// c1.setPaddingBottom(5f);
		// c1.setPaddingRight(3f);
		c1.setPadding(5f);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidth(0);
		c1.setCellEvent(event);
		table.addCell(c1);

		// sel 6
		p = new Paragraph();
		ch = new Chunk("+", FontFactory.getFont(FontFactory.HELVETICA, 7,
				Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setPadding(0);
		table.addCell(c1);

		// sel 7
		p = new Paragraph();
		c1 = new PdfPCell(p);
		t = new String[] { "Koreksi |", "Adjustment ", "1.000" };
		c1.addElement(addSummaryInfo(t[0], t[1], t[2]));
		// c1.setPaddingBottom(5f);
		// c1.setPaddingRight(3f);
		c1.setPadding(5f);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidth(0);
		c1.setCellEvent(event);
		table.addCell(c1);

		// sel 8
		p = new Paragraph();
		ch = new Chunk("=", FontFactory.getFont(FontFactory.HELVETICA, 7,
				Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setPadding(0);
		table.addCell(c1);

		// sel 3
		p = new Paragraph();
		c1 = new PdfPCell(p);
		t = new String[] { "Sisa Tagihan |", "Balance Due ", "11.100" };
		c1.addElement(addSummaryInfo(t[0], t[1], t[2]));
		// c1.setPaddingBottom(5f);
		// c1.setPaddingRight(3f);
		c1.setPadding(5f);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidth(0);
		c1.setCellEvent(event);
		table.addCell(c1);

		// new line
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setColspan(3);
		table.addCell(c1);

		document.add(table);

	}

	private static PdfPTable addSummaryInfo(String t1, String t2, String t3)
			throws DocumentException {

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		table.setWidths(new int[] { 18, 82 });
		// sel 1
		Paragraph p = new Paragraph();
		Chunk ch = new Chunk(t1, FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		PdfPCell c1 = new PdfPCell(p);
		c1.setColspan(2);
		c1.setBorderWidth(0);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(c1);

		// sel 2
		p = new Paragraph();
		ch = new Chunk(t2, FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setColspan(2);
		c1.setBorderWidth(0);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(c1);

		// sel 3
		p = new Paragraph();
		ch = new Chunk("Rp.", FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setPaddingBottom(0.8f);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(c1);

		// sel 4
		p = new Paragraph();
		ch = new Chunk(t3, FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		c1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		c1.setBorderWidthBottom(0.1f);
		c1.setPaddingBottom(0.8f);
		c1.setBorderWidth(0);
		table.addCell(c1);

		return table;

	}

}
